package com.mask.im.server.netty.action.handler;

import com.alibaba.fastjson.JSON;
import com.mask.im.server.netty.IMChannelCtxMap;
import com.mask.im.server.netty.enums.IMActionEnum;
import com.mask.im.server.netty.model.IMReceiveMessage;
import com.mask.im.server.netty.model.IMSendMessage;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.AttributeKey;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @className： MaskFriendStatusSocketHandler
 * @Description: 好友上下线通知
 * @Author：jian
 * @Date：2025/8/29 11:39
 */

@Log4j2
@Component
public class IMFriendStatusHandler extends IMAbstractChatHandler {

    @Override
    public IMActionEnum getAction() {
        return IMActionEnum.FRIEND_STATUS;
    }

    @Override
    public void handle(IMReceiveMessage imReceiveMessage, Channel channel) {
        Integer extend = imReceiveMessage.getExtend();
        long startTime = System.currentTimeMillis();
        log.info("收到上下线通知请求，extend参数: [{}]", extend);
//        if (Objects.nonNull(extend)) {
//            Integer flag = extend;
//            AttributeKey<Long> attributeKey = AttributeKey.valueOf("USER_ID");
//            Long userIdLong = channel.attr(attributeKey).get();
//            if (null != userIdLong) {
//                String userId = String.valueOf(userIdLong);
////                List<String> friendIds = friendshipsService.byUserId(userId);
//                List<String> friendIds = new ArrayList<>();
//                if (null != friendIds && !friendIds.isEmpty()) {
//                    log.info("用户[{}]的好友列表: [{}]", userId, JSON.toJSONString(friendIds));
//                    IMSendMessage websocketDTO = new IMSendMessage();
//                    if (flag.equals(0)) {
//                        websocketDTO.setCmd(3000);
//                        websocketDTO.setIsActive(0);
//                        websocketDTO.setOnAndOffLineUserId(userId);
//                        IMChannelCtxMap.userIdChannel.forEach((k, v) -> {
//                            if (friendIds.contains(k)) {
//                                v.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO)));
//                            }
//                        });
//                    } else {
//                        initChannel(channel);
//                        websocketDTO.setCmd(30000);
//                        websocketDTO.setIsActive(1);
//                        websocketDTO.setOnAndOffLineUserId(userId);
//                        IMChannelCtxMap.userIdChannel.forEach((k, v) -> {
//                            if (friendIds.contains(k)) {
//                                v.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO)));
//                            }
//                        });
//                    }
//
//                }
//            }
//            log.info("上下线通知处理完成userId:[{}],status:[{}],costMs:[{}]", userIdLong
//                    , flag.equals(1) ? "online" : "offline"
//                    , System.currentTimeMillis() - startTime);
//        }
    }

    private void initChannel(Channel channel) {
        String id = channel.id().asShortText();
        IMChannelCtxMap.setUserIdAndIdBindChannel(id, channel);
        IMChannelCtxMap.setChannelBindUserId(id,channel);
        log.info("OnAndOffLineNotificationsImpl---------notifyFriends---------initChannel:[{}]", id);
    }
}
